package com.anis.project.medcare.dicom.file.selector;

import java.io.File;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;

import com.anis.project.medcare.Activator;
import com.anis.project.medcare.views.DicomViewer;

public class DicomFileSelector extends Dialog {
	private Text fileLocationSource;
	private Point initialSize;
	private Group grpConfiguration;
	private Composite container;
	private Text serverAddress;
	private Text portNumber;
	private String pathToDicomFile;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */
	public DicomFileSelector(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1, true));

		Group grpSource = new Group(container, SWT.BORDER
				| SWT.SHADOW_ETCHED_OUT);
		grpSource.setLayout(new GridLayout(2, false));
		grpSource.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true,
				1, 1));
		grpSource.setText("Source");

		final Button btnLocalFile = new Button(grpSource, SWT.RADIO);

		btnLocalFile.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false, 1, 1));
		btnLocalFile.setText("Local file");

		final Button btnNetworkLocation = new Button(grpSource, SWT.RADIO);

		btnNetworkLocation.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false, 1, 1));
		btnNetworkLocation.setText("Network location");

		grpConfiguration = new Group(container, SWT.BORDER
				| SWT.SHADOW_ETCHED_OUT);
		grpConfiguration.setLayout(new GridLayout(4, false));
		grpConfiguration.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				true, 1, 1));
		grpConfiguration.setText("Configuration");

		initialSize = grpConfiguration.getSize();

		btnLocalFile.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if ((grpConfiguration != null)
						&& (!grpConfiguration.isDisposed())) {
					grpConfiguration.dispose();
				}
				grpConfiguration = new Group(container, SWT.BORDER
						| SWT.SHADOW_ETCHED_OUT);
				grpConfiguration.setLayout(new GridLayout(3, false));
				grpConfiguration.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
						false, true, 1, 1));
				grpConfiguration.setText("Configuration");
				Label lblFileLocation = new Label(grpConfiguration, SWT.NONE);
				lblFileLocation.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
						false, false, 1, 1));
				lblFileLocation.setText("File location :");

				fileLocationSource = new Text(grpConfiguration, SWT.BORDER);
				fileLocationSource.setEditable(false);
				fileLocationSource.setLayoutData(new GridData(SWT.FILL,
						SWT.FILL, true, false, 1, 1));
				fileLocationSource.addModifyListener(new ModifyListener() {

					@Override
					public void modifyText(ModifyEvent e) {
						pathToDicomFile = fileLocationSource.getText();
						getOKButton().setEnabled(!pathToDicomFile.isEmpty());
					}
				});
				Button btnBrowse = new Button(grpConfiguration, SWT.NONE);
				btnBrowse.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
						false, 1, 1));
				btnBrowse.setText("Browse");
				btnBrowse.addSelectionListener(new SelectionListener() {

					@Override
					public void widgetSelected(SelectionEvent e) {
						Shell parentShell = Activator.getDefault()
								.getWorkbench().getActiveWorkbenchWindow()
								.getShell();
						DirectoryDialog fileDialog = new DirectoryDialog(
								parentShell);

						if (fileDialog.open() != null
								&& fileLocationSource != null)
							fileLocationSource.setText(fileDialog.open());
					}

					@Override
					public void widgetDefaultSelected(SelectionEvent e) {
						// TODO Auto-generated method stub

					}
				});
				grpConfiguration.setSize(0, 0);
				grpConfiguration.setSize(initialSize);
				Point containerSize = container.getSize();
				container.setSize(0, 0);
				container.setSize(containerSize);
			}
		});

		btnNetworkLocation.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if ((grpConfiguration != null)
						&& (!grpConfiguration.isDisposed())) {
					grpConfiguration.dispose();
				}
				grpConfiguration = new Group(container, SWT.BORDER
						| SWT.SHADOW_ETCHED_OUT);
				grpConfiguration.setLayout(new GridLayout(4, false));
				grpConfiguration.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
						false, true, 1, 1));
				grpConfiguration.setText("Configuration");

				Label lblServerAddress = new Label(grpConfiguration, SWT.NONE);
				lblServerAddress.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
						false, false, 1, 1));
				lblServerAddress.setText("Server Address  :");

				serverAddress = new Text(grpConfiguration, SWT.BORDER);
				serverAddress.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
						true, false, 1, 1));

				Label lblPort = new Label(grpConfiguration, SWT.NONE);
				lblPort.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
						false, 1, 1));
				lblPort.setText("Port :");

				portNumber = new Text(grpConfiguration, SWT.BORDER);
				portNumber.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
						false, false, 1, 1));

				Point point = grpConfiguration.getSize();
				grpConfiguration.setSize(0, 0);
				grpConfiguration.setSize(point);

				Point containerSize = container.getSize();
				container.setSize(0, 0);
				container.setSize(containerSize);
			}
		});

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		getOKButton().setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}

	@Override
	protected void okPressed() {
		super.okPressed();
		try {
			Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
					.getActivePage().showView(DicomViewer.ID);
			Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
					.getActivePage().setEditorAreaVisible(false);
			DicomViewer dicomViewer = (DicomViewer) Activator.getDefault()
					.getWorkbench().getActiveWorkbenchWindow().getActivePage()
					.findView(DicomViewer.ID);
			dicomViewer.setDirectorySource(pathToDicomFile);
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
